package com.ease.actionscript.mapping.mapper;

import java.lang.reflect.Method;

import org.springframework.util.StringUtils;

/**
 * 
 * @author nathanleewei
 * 
 */
public class ValueObjectInterfaceMapper extends AbstractValueObjectClassMapper {

	public boolean isCompliant() {
		return super.isCompliant();
	}

	public void prepare() {
		super.prepare();

		Method[] declaredMethods = clazz.getDeclaredMethods();

		for (Method method : declaredMethods) {
			String methodName = method.getName();
			Class<?>[] parameterTypes = method.getParameterTypes();

			int parameterTypeSize = 0;
			if (null != parameterTypes) {
				parameterTypeSize = parameterTypes.length;
			}

			if ((methodName.startsWith("set") && 1 == parameterTypeSize) || //
					(methodName.startsWith("get") && 0 == parameterTypeSize)) {

				this.addClassMethod(method);

				for (Class<?> parameterType : parameterTypes) {
					String parameterTypeName = getActionScriptTypeName(parameterType);
					if (null != parameterTypeName && parameterTypeName.indexOf('.') > 0) {
						this.addImportClassName(parameterTypeName);

					}
				}
				Class<?> returnType = method.getReturnType();
				String returnTypeName = getActionScriptTypeName(returnType);
				if (null != returnTypeName && returnTypeName.indexOf('.') > 0) {
					this.addImportClassName(returnTypeName);
				}
			}
		}
	}

	public String getInterfaceVoMethodName(Method method) {
		String methodName = method.getName();
		StringBuilder sb = new StringBuilder();
		sb.append(methodName.substring(0, 3));
		sb.append(" ");
		sb.append(StringUtils.uncapitalize(methodName.substring(3)));

		return sb.toString();
	}

}